package xman.mssh.hibernate.entity;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "tb_one_ref")
public class OneReferEntity {
    
    /*-
     * 谁可以掌握主动权，谁就可以配置关联关系
     * 
     * 单向一对多只需要在一端配置（单向关联）
     * 双向一对多需要在关联双方都配置（双向关联）
     * 
     * OneToMany关系中，将Many一方设置为主控方将有助于性能的改善
     * 通过inverse可以指定谁是主控方，及关联关系由谁维护（主控方关联更新，被控方不会关联更新,注意建立关系的方向ref.set(mapped)）
     * 
     * cascade涉及到被关联的对象的维护（两个对象的更新策略）
     * 
     * mappedBy（定义不维护对象间的关联关系，不代表不使用关联关系）,
     * mappedBy定义表示当前对象是主表,被关联者,用在OneToOne,OneToMany,ManyToMany
     * mappedBy跟JoinColumn/JoinTable互斥
     * 
     * "一"方：
     * @OneToMany --> mappedBy:"多"方的关联属性(被控方)(定义在被控方，指向主控方),mappedBy相当于过去的inverse="true",inverse＝true端无须维护这些关系。
     * "多"方：
     * @ManyToOne --> @JoinColumn,"多"方定义的外键字段, FOREIGN KEY (classid) REFERENCES classes(id)  => @JoinColumn(name="classid") 
     * 
     * 
     * @ManyToMany多对多，是双向关联，理论上不存在主动方和被动方之分,所以理论上是不存在mappedby的概念了，使用mappedby注解来强调不维护关联关系
     * 
     * @JoinColumn(设置外键)
     * name（外键的字段名）:column_refer_name,可以手动指定
     * referencedColumnName（外键对应主表的字段）:refer_table(column_name)，可以手动指定
     * optional:设置关联实体是否必须存在。false（inner join）必须存在 即不是随意的，true(left join)关联实体可以不存在即是随意的,默认是true
     */

    @Id
    @Column
    @GeneratedValue
    private Integer id;
    
    // 单向
    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)
    @JoinColumn
    private One2OneEntity one;
    
    // 双向
    @OneToOne(cascade = CascadeType.ALL)
    private One2OneMappedEntity oneMapped;
    
    // 单向
    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
    private OneManyEntity many;
    
    // 双向
    @ManyToOne(cascade = { CascadeType.ALL })
    @JoinColumn
    private OneManyMappedEntity manyMapped;
    
    private String descr;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date utime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    
    public String getDescr() {
        return descr;
    }
    
    public void setDescr(String descr) {
        this.descr = descr;
    }
    
    public One2OneEntity getOne() {
        return one;
    }
    
    public void setOne(One2OneEntity one) {
        this.one = one;
    }

    public Date getUtime() {
        return utime;
    }
    
    public void setUtime(Date utime) {
        this.utime = utime;
    }
    
    public One2OneMappedEntity getOneMapped() {
        return oneMapped;
    }
    
    public void setOneMapped(One2OneMappedEntity oneMapped) {
        this.oneMapped = oneMapped;
    }
    
    public OneManyMappedEntity getManyMapped() {
        return manyMapped;
    }
    
    public void setManyMapped(OneManyMappedEntity manyMapped) {
        this.manyMapped = manyMapped;
    }
    
    public OneManyEntity getMany() {
        return many;
    }
    
    public void setMany(OneManyEntity many) {
        this.many = many;
    }

}
